package cn.ipenta.jkb.repository;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSON;

import cn.ipenta.blockchain.entity.TransactionInfo;
import cn.ipenta.blockchain.repo.AbstractBlockChainSource;
import cn.ipenta.jkb.JkbContants;
import cn.ipenta.jkb.entity.TxInfo;

@Repository
public class TxInfoBlockChainSource extends AbstractBlockChainSource<TxInfo> {

	@Autowired
	TxInfoRepository txInfoRepository;

	@Autowired
	MongoTemplate mongoTemplate;

	@Override
	protected String nameRecordType() {
		return JkbContants.RECORD_TXINFO;
	}
	
	public TxInfo findByTypeAndReceiverId(String type,String id) {
		Criteria criteria = Criteria.where("contentInfo.recordContent.receiverId").is(id);
		System.out.println(JSON.toJSON(mongoTemplate.find(new Query(criteria), TransactionInfo.class)));
		return null;
	}


	public List<TxInfo> findRecordBySenderId(String id) {
		Criteria criteria = Criteria.where("contentInfo.recordContent.senderId").is(id);
		return convertList(mongoTemplate.find(new Query(criteria), TransactionInfo.class), new TxInfo());
	}

	public List<TxInfo> findRecordByReceiverId(String id) {
		Criteria criteria = Criteria.where("contentInfo.recordContent.receiverId").is(id);
		return convertList(mongoTemplate.find(new Query(criteria), TransactionInfo.class), new TxInfo());
	}

}
